#pragma once

class CEX DateTime:public Object
{
	struct tm _tm;

public:

	DateTime()
	{
		fromTime(time(NULL));
	}

	DateTime(time_t t)
	{
		fromTime(t);
	}

	DateTime(const DateTime& o)
	{
		memcpy(&_tm,&(o._tm),sizeof(struct tm));
	}

	DateTime(struct tm* t)
	{
		memcpy(&_tm,t,sizeof(struct tm));
	}

	~DateTime()
	{
	}

	void fromTime(time_t t)
	{
		Platform::localTime(t,&_tm);
	}

	time_t toTime() const
	{
		struct tm ttm;
		memcpy(&ttm,&_tm,sizeof(struct tm));

		return mktime(&ttm);
	}

	DateTime& operator=(const DateTime& o)
	{
		memcpy(&_tm,&(o._tm),sizeof(struct tm));
		
		return *this;
	}
	
	bool operator<(const DateTime& o) 	const { return toTime()<o.toTime(); }
	bool operator<=(const DateTime& o) 	const { return toTime()<=o.toTime(); }
	bool operator>(const DateTime& o) 	const { return toTime()>o.toTime(); }
	bool operator>=(const DateTime& o) 	const { return toTime()>=o.toTime(); }
	bool operator==(const DateTime& o) 	const { return toTime()==o.toTime(); }
	bool operator!=(const DateTime& o) 	const { return toTime()!=o.toTime(); }

	int getYear()	const { return _tm.tm_year+1900; }
	int getMonth()	const { return _tm.tm_mon+1; }
	int getDay()	const { return _tm.tm_mday; }
	int getHour()	const { return _tm.tm_hour; }
	int getMin()	const { return _tm.tm_min; }
	int getSec()	const { return _tm.tm_sec; }
	int getWeekDay()const { return _tm.tm_wday; }
	int getYearDay()const { return _tm.tm_yday; }
	bool getDST()	const { return _tm.tm_isdst>0; }

	void setDate(int y,int m,int d)
	{
		_tm.tm_year = y-1900;
		_tm.tm_mon = m-1;
		_tm.tm_mday = d;

		fromTime(toTime());
	}

	void setTime(int h,int m,int s)
	{
		_tm.tm_hour = h;
		_tm.tm_min = m;
		_tm.tm_sec = s;

		fromTime(toTime());
	}

	String getDateStr() const
	{
		return String::format("%04i-%02i-%02i",getYear(),getMonth(),getDay());
	}

	String getTimeStr() const
	{
		return String::format("%02i:%02i:%02i",getHour(),getMin(),getSec());
	}

	String getDateTimeStr() const
	{
		return String::format("%04i-%02i-%02i @ %02i:%02i:%02i",getYear(),getMonth(),getDay(),getHour(),getMin(),getSec());
	}

	String getTimeStamp() const
	{
		return String::format("%04i%02i%02i%02i%02i%02i",getYear(),getMonth(),getDay(),getHour(),getMin(),getSec());
	}

	void addYear(int y)
	{
		_tm.tm_year += y;
		fromTime(toTime());
	}

	void addMonth(int m)
	{
		_tm.tm_mon += m;
		fromTime(toTime());
	}

	void addDay(int d)
	{
		_tm.tm_mday += d;
		fromTime(toTime());
	}

	void addHour(int h)
	{
		_tm.tm_hour += h;
		fromTime(toTime());
	}

	void addMin(int m)
	{
		_tm.tm_min += m;
		fromTime(toTime());
	}

	void addSec(int s)
	{
		_tm.tm_sec += s;
		fromTime(toTime());
	}

	static DateTime now()
	{
		return DateTime();
	}

};



